/*
 * MainApp.java
 */

package hi.chyl.json;

import java.awt.Font;
import java.awt.FontFormatException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Properties;
import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;
import org.openide.util.Exceptions;

/**
 * The main class of the application.
 */
public class MainApp extends SingleFrameApplication {
    public static Properties conf = new Properties();
    //public static final Font FONT_FIXEDSYS;
    static {
        System.setProperty("awt.useSystemAAFontSettings", "off");  
        //System.setProperty("swing.aatext", "true"); 
        //FONT_FIXEDSYS = loadFont("FSEX300.ttf");
    }
    
    public static Font loadFont(String file)
    {
        InputStream in = null;
        try
        {
            in = Application.getInstance().getClass().getResourceAsStream("/font/" + file);
            Font f = Font.createFont(Font.TRUETYPE_FONT, in);
            Font df = f.deriveFont(16.0F);
            return df;
        } catch (FontFormatException ex)
        {
            Exceptions.printStackTrace(ex);
        } catch (IOException ex)
        {
            Exceptions.printStackTrace(ex);
        } finally {
            if (null != in)
            {
                try
                {
                    in.close();
                } catch (IOException ex)
                {
                    Exceptions.printStackTrace(ex);
                }
            }
        }
        return null;
    }
    
    public static void loadConfig()
    {
        try
        {
            File file = new File("conf.ini");
            if (!file.exists())
            {
                file.createNewFile();
            }
            conf.load(new InputStreamReader(new FileInputStream(file), "UTF-8"));
            conf.list(System.out);
        } catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }
    
    public static void saveConfig()
    {
        Writer writer = null;
        try
        {
            File file = new File("conf.ini");
            if (!file.exists())
            {
                file.createNewFile();
            }
            writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
            conf.store(writer, null);
        } catch (Exception ex)
        {
            ex.printStackTrace();
        } finally
        {
            try
            {
                writer.close();
            } catch (IOException ex)
            {
                ex.printStackTrace();
            }
        }
    }

    //public static final ResourceMap RESOURCE = Application.getInstance(MainApp.class).getContext().getResourceMap(MainView.class);
    /**
     * At startup create and show the main frame of the application.
     */
    @Override protected void startup() {
        show(new MainView(this));
    }


    @Override
    protected void initialize(String[] args)
    {
        super.initialize(args);
        loadConfig();
    }

    @Override
    protected void shutdown()
    {
        saveConfig();
        super.shutdown();
    }
    

    /**
     * This method is to initialize the specified window by injecting resources.
     * Windows shown in our application come fully initialized from the GUI
     * builder, so this additional configuration is not needed.
     */
    @Override protected void configureWindow(java.awt.Window root) {
    }

    /**
     * A convenient static getter for the application instance.
     * @return the instance of MainApp
     */
    public static MainApp getApplication() {
        return Application.getInstance(MainApp.class);
    }

    /**
     * Main method launching the application.
     */
    public static void main(String[] args) {
        launch(MainApp.class, args);
    }
}
